package com.coinworld;

import com.google.common.collect.Maps;
import com.coinworld.filter.XssFilter;
import com.coinworld.utils.ZbannerPrint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Map;

@EnableScheduling
@ImportResource(locations = {"classpath:spring-config/*.xml"})
@ServletComponentScan
@SpringBootApplication
public class HoneyTripApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(HoneyTripApplication.class, args);
		ZbannerPrint.printLogo();
	}
	@Override//为了打包springboot项目
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder builder) {

		return builder.sources(this.getClass());
	}

	@Bean
	public FilterRegistrationBean xssFilterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new XssFilter());
		filterRegistrationBean.setOrder(1);
		filterRegistrationBean.setEnabled(true);
		filterRegistrationBean.addUrlPatterns("/*");
		Map<String, String> initParameters = Maps.newHashMap();
		//-excludes用于配置不需要参数过滤的请求url;
		//-isIncludeRichText默认为true，主要用于设置富文本内容是否需要过滤。
		initParameters.put("excludes", "");
		initParameters.put("isIncludeRichText", "true");
		filterRegistrationBean.setInitParameters(initParameters);
		return filterRegistrationBean;
	}

	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder) {
		RestTemplate restTemplate = builder.build();
		restTemplate.getMessageConverters()
				.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
		return restTemplate;
	}

}
